package com.msb.dongbao.portal.web.controller.studyCaptcha;

import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author: mayu
 * @date: 2021年05月28日 14:55
 */
@RestController
@RequestMapping("/easy-captcha")
public class EasyCaptchaController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/generator")
    public void generatorCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            //CaptchaUtil.out(request, response);

            //算术验证码
            //ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(200, 100, 3);
            //CaptchaUtil.out(arithmeticCaptcha, request, response);

            //中文验证码
            ChineseCaptcha chineseCaptcha = new ChineseCaptcha(200, 100, 4);
            CaptchaUtil.out(chineseCaptcha, request, response);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/verify")
    public String verify(String verifyCode, HttpServletRequest request) {
        boolean flag = CaptchaUtil.ver(verifyCode, request);
        if (flag) {
            CaptchaUtil.clear(request);
            return "通过";
        }
        return "不通过";
    }

    @GetMapping("/generator-redis")
    public Map<String, String> generatorCodeRedis(HttpServletRequest request, HttpServletResponse response) {
        SpecCaptcha specCaptcha = new SpecCaptcha(100, 50);

        String text = specCaptcha.text();
        System.out.println("验证码：" + text);

        //String sessionId = request.getSession().getId();
        //redisTemplate.opsForValue().set(sessionId, text);

        String uuid = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(uuid, text);
        //验证码1分钟后过期
        redisTemplate.expire(uuid, 60, TimeUnit.SECONDS);

        String base64 = specCaptcha.toBase64();
        Map<String, String> map = new HashMap<>();
        map.put("uuid", uuid);
        map.put("base64", base64);

        return map;

        /*try {
            CaptchaUtil.out(specCaptcha, request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    @GetMapping("/verify-redis")
    public String verifyRedis(String verifyCode, String uuid, HttpServletRequest request) {
        //String sessionId = request.getSession().getId();
        //String c = redisTemplate.opsForValue().get(sessionId);

        String c = redisTemplate.opsForValue().get(uuid);

        if (verifyCode.equals(c)) {
            //redisTemplate.delete(sessionId);
            //redisTemplate.delete(uuid);
            return "通过";
        }
        return "不通过";
    }
}
